package co.yixiang.huiMin.controller;


import co.yixiang.annotation.AnonymousAccess;
import co.yixiang.huiMin.dao.YxUserWithdrawQueryCriteria;
import co.yixiang.huiMin.entity.YxUserWithdraw;
import co.yixiang.huiMin.repository.UserWithdrawRepository;
import co.yixiang.huiMin.service.YxUserWithdrawService;
import co.yixiang.modules.shop.domain.YxUser;
import co.yixiang.modules.shop.domain.YxUserBill;
import co.yixiang.modules.shop.service.YxUserBillService;
import co.yixiang.modules.shop.service.YxUserService;
import co.yixiang.modules.shop.service.dto.YxUserDTO;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 用户提现 前端控制器
 * </p>
 *
 * @author Author
 * @since 2020-06-30
 */
@RestController
@Api(tags = "用户提现")
@RequestMapping("/yxUserWithdraw")
public class YxUserWithdrawController {

    @Resource
    private YxUserWithdrawService yxUserWithdrawService;

    @Resource
    private YxUserService yxUserService;

    @Resource
    private YxUserBillService yxUserBillService;

    @Resource
    private UserWithdrawRepository userWithdrawRepository;


    /**
     * 查询审核的提现
     * @return
     */
    @AnonymousAccess
    @ApiOperation("查询审核的提现")
    @GetMapping("/selectYxUserWithdraws")
    public ResponseEntity<Object> selectYxUserWithdraws(Pageable pageable, YxUserWithdrawQueryCriteria yxUserWithdrawQueryCriteria){

        Page<YxUserWithdraw> page = userWithdrawRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, yxUserWithdrawQueryCriteria, criteriaBuilder), pageable);
        List<YxUserWithdraw> list = page.getContent();
        for (YxUserWithdraw yxUserWithdraw : list) {

            YxUserDTO yxUser=yxUserService.findById(yxUserWithdraw.getUserId());
            if(yxUser.getNickname()!=null){
                yxUserWithdraw.setNickname(yxUser.getNickname());
            }
            if(yxUser.getAvatar()!=null){
                yxUserWithdraw.setAvatar(yxUser.getAvatar());
            }
        }
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("code",200);
        stringObjectHashMap.put("message",PageUtil.toPage(page));
        return new ResponseEntity<>(stringObjectHashMap, HttpStatus.OK);

    }

    /**
     * 审核不通过
     * @return
     */
    @AnonymousAccess
    @ApiOperation("审核不通过")
    @PostMapping("/updateYxUserWithdrawsNo")
    public ResponseEntity<Object> updateYxUserWithdrawsNo(YxUserWithdraw yxUserWithdraw){

        YxUserWithdraw yxUserWithdraws=yxUserWithdrawService.getById(yxUserWithdraw.getWithdrawId());
        yxUserWithdraws.setWithdrawType(3);//不通过
        int count=yxUserWithdrawService.updateYxUserWithdraws(yxUserWithdraws);
         if(count>0){
             //查询用户信息
             YxUser yxUser=yxUserService.findByIds(yxUserWithdraws.getUserId());
             yxUser.setNowMoney(yxUser.getNowMoney().add(yxUserWithdraws.getWithdrawPrice()));
             yxUserService.update(yxUser);
             //插入收支记录
             YxUserBill yxUserBill=new YxUserBill();
             yxUserBill.setUid(yxUserWithdraws.getUserId());
             yxUserBill.setPm(1);//获得
             yxUserBill.setTitle("提现退回");
             yxUserBill.setCategory("now_money");
             yxUserBill.setBillType("product_refund");
             yxUserBill.setBillNumber(yxUserWithdraws.getWithdrawPrice());
             yxUserBill.setBalance(yxUser.getNowMoney());
             yxUserBill.setMark("提现失败退回金额"+yxUserWithdraws.getWithdrawPrice());
             yxUserBill.setAddTime(LocalDateTime.now());
             yxUserBillService.create(yxUserBill);
             return new ResponseEntity<>(200, HttpStatus.OK);
         }
        return new ResponseEntity<>(300, HttpStatus.OK);
    }


    /**
     * 审核通过
     * @return
     */
    @ApiOperation("审核通过")
    @AnonymousAccess
    @PostMapping("/updateYxUserWithdrawsYes")
    public ResponseEntity<Object> updateYxUserWithdrawsYes(YxUserWithdraw yxUserWithdraw){

        YxUserWithdraw yxUserWithdraws=yxUserWithdrawService.getById(yxUserWithdraw.getWithdrawId());
        yxUserWithdraws.setWithdrawType(2);//通过
        int count=yxUserWithdrawService.updateYxUserWithdraws(yxUserWithdraws);
        if(count>0){
            return new ResponseEntity<>(200, HttpStatus.OK);
        }
        return new ResponseEntity<>(300, HttpStatus.OK);
    }

}

